<template>
  <div class="comment">
    <div class="comment_user">
      <div class="user_img">
        <img
          v-if="comment.account.defaultAvatar"
          :src="$axios.defaults.baseURL + comment.account.defaultAvatar"
          alt
        />
        <img v-else src="@/assets/img/pic_sea.jpeg" alt />
      </div>
      <span class="user_nickname">{{comment.account.nickname}}</span>
      <p class="time">
        <span>{{timeYears}}</span>
        <span>{{timeMinute}}</span>
      </p>
    </div>
    <!-- 递归子组件 -->
    <parent v-if="comment.parent" :parent="comment.parent" @commentReply='commentReply'></parent>
    <!-- 递归子组件 -->
    <div class="comment_main">
      <p class="comment_text">{{comment.content}}</p>
      <div class="comment_main_img">
        <!-- <div v-if="comment.pics.length>0"></div> -->
        <!-- 图片预览组件 -->
        <el-image
          v-for="(item,index) of comment.pics"
          :key="index"
          style="width: 100px; height: 100px"
          :src="''+$axios.defaults.baseURL + item.url"
          :preview-src-list="initImgUrl"
        ></el-image>
      </div>
    </div>
    <div class="reply" @click="commentReply(comment)">回复</div>
  </div>
</template>

<script>
import moment from "moment";
import parent from "./parent";
export default {
  props: ["comment"],
  computed: {
    // 时间函数
    timeYears() {
      const years = moment(this.comment.created_at).format("YYYY-MM-DD");
      return years;
    },
    timeMinute() {
      const minute = moment(this.comment.created_at).format("HH:mm");
      return minute;
    },
    // el-image 图片 路径初始化
    initImgUrl() {
      return this.comment.pics.map(item => {
        return "" + this.$axios.defaults.baseURL + item.url;
      });
    }
  },
  methods: {
    commentReply(obj){
      // console.log('id: ', id);
      this.$emit('replyWho',obj)
    }
  },
  mounted() {
    // if (this.comment.pics.length > 0) {
    //   console.log("" + this.$axios.defaults.baseURL + this.comment.pics[0].url);
    // }
  },
  components: {
    parent
  }
};
</script>

<style lang="less" scoped>
.comment {
  padding: 15px 0 30px 0;
  border-bottom: 1px dashed #dddddd;
  // display: flex;
  position: relative;
  .comment_user {
    display: flex;

    font-size: 12px;
    .user_img {
      width: 16px;
      height: 16px;
      padding: 0 8px;
      img {
        width: 100%;
        height: auto;
      }
    }
    .time {
      padding: 0 5px;
      color: #999999;
    }
  }

  .comment_main {
    .comment_text {
      padding: 10px;
    }
    // 图片
    /deep/ .el-image {
      width: 80px !important;
      height: 80px !important;
      padding: 5px;
      margin: 0 3px;
      border: 1px dashed #ddd;
      /deep/ img {
        width: 100%;
        height: 100%;
      }
    }
  }
}
// 回复
.reply {
  cursor: pointer;
  font-size: 12px;
  color: #1e50a2;
  position: absolute;
  right: 20px;
  bottom: 10px;
}
</style>